//1.固定的，引进express框架
const express = require('express')
const cors = require('cors')

const app = express()
app.use(
    cors({
      origin: "*",
    })
);
//4.连接数据库
var mysql = require('mysql');
var pool = mysql.createPool({
    host: 'localhost',
    user: 'root',
    password: '',
    database: 'mac'
})
app.use(express.json())
// 将post传递参数转为对象
app.use(express.urlencoded({
	extended:false
}))
//3.端口号
const port = 3000
//3.1 监听端口
app.listen(port, () => {
    console.log(`Example app listening at http://localhost:${port}`)
})

//实际使用中可以在应用启动时进行初始化(只需执行一次)
// require('mysql-queries').init(pool);

//  //执行多条SQLs
//  var pool = require('mysql-queries');
app.use((err,req,res,next)=>{
    console.log(err)
    res.send({code:500,msg:'服务器端错误'})
})
//接口测试
app.get('/chat',(req,res)=>{
    res.send('hello world')
})
//查询所有底妆 ttp://localhost:3000/search/dz
app.get('/search/dz',(req,res)=>{
    let sql='select * from dz'
    pool.query(sql,(err, result)=>{
        if(err){
            throw err
        }
        res.send({code:200,msg:'查询成功',data: result})
    })
})
//查询底妆按分类 http://localhost:3000/search/dz
app.get('/search/dz/:lx',(req,res)=>{
    let obj = req.params
    let sql='select * from dz where lx=?'
    pool.query(sql,[obj.lx],(err, result)=>{
        if(err){
            throw err
        }
        res.send({code:200,msg:'查询成功',data: result})
    })
    
})
//查询底妆名字详情 http://localhost:3000/detail/dz
app.get('/detail/dz/:name',(req,res)=>{
    let obj = req.params
    let sql='select * from dz where name=?'
    pool.query(sql,[obj.name],(err, result)=>{
        if(err){
            throw err
        }
        res.send({code:200,msg:'查询成功',data: result})
    })
})
//增加一条底妆 http://localhost:3000/add/dz
app.post('/add/dz',(req,res)=>{
    let obj = req.body
    let sql='insert into dz set ?'
    pool.query(sql,[obj],(err,result)=>{
        if(err) {
            throw err
        }
        res.send({code:200,msg:'添加成功',data:result})
    })
})


//修改一条底妆 http://localhost:3000/search/cz
app.post('/upd/dz',(req,res)=>{
    // let obj=req.body
    let did=req.query.did
    let obj = Object.assign({},req.body)
    console.log(obj)
     let sql='update dz set ? where did=?'
    pool.query(sql,[obj,did],(err,result)=>{
        if(err) {
            throw err
        }
        console.log(result)
		// 成功结果是对象，如果对象下的changedRows值为0说明修改失败，否则修改成功
		if(result.changedRows===0){
			res.send({code:400,msg:'修改失败'})
		}else{
			res.send({code:200,msg:'修改成功'})
		}
    }) 
})
//删除一条底妆 http://localhost:3000/del/cz
app.post('/del/dz',(req,res)=>{
    let did=req.query.did
    console.log(did)
    res.send('删除数据')
    let sql='delete from dz where did=?'
    pool.query(sql,[did],(err,result)=>{
        if(err) {
            throw err
        }
		// 删除结果是对象，判断对象下affectedRows值
		// 判断是否删除成功
		if(result.affectedRows===0) {
			res.send({code:400,msg:'删除失败'})
		}else{
            console.log(result)
            res.send({code:200,msg:'删除成功'})
        }
    })
})
/**
 * 通过商品名称关键字模糊查询所有商品
 */

/*  app.get("/search/name",(req, res) => {
    let name = req.query.name
    let sqls = ['select * from cz where name like ?','select * from dz where name like ?','select * from yz where name like ?','select * from sj where name like ?','select * from hf where name like ?','select * from pro where name like ?']
    pool.queries(sqls,[[`%${name}%`],[`%${name}%`],[`%${name}%`],[`%${name}%`],[`%${name}%`],[`%${name}%`]], (err, results)=>{
      if(err) {
        console.log(err);
      } else {
        //"results"为数组,其为多条SQL的执行结果.
        // console.log(results);
        res.send({code:200,msg:'查询成功',data:results})
      }
    });
}) */

//查询所有唇妆 http://localhost:3000/search/cz
app.get('/search/cz',(req,res)=>{
    let sql='select * from cz'
    pool.query(sql,(err, result)=>{
        if(err){
            throw err
        }
        res.send({code:200,msg:'查询成功',data: result})
    })
})
//查询唇妆名字详情 http://localhost:3000/detail/cz
app.get('/detail/cz/:name',(req,res)=>{
    let obj = req.params
    let sql='select * from cz where name=?'
    pool.query(sql,[obj.name],(err, result)=>{
        if(err){
            throw err
        }
        res.send({code:200,msg:'查询成功',data: result})
    })
})
//增加一条唇妆 http://localhost:3000/add/cz
app.post('/add/cz',(req,res)=>{
    let obj = req.body
    let sql='insert into cz set ?'
    pool.query(sql,[obj],(err,result)=>{
        if(err) {
            throw err
        }
        res.send({code:200,msg:'添加成功',data:result})
    })
})


//修改一条唇妆 http://localhost:3000/upd/cz
app.post('/upd/cz',(req,res)=>{
    // res.send('修改数据')
    // let obj=req.body
    let obj = Object.assign({},req.body)
    let cid=req.query.cid
    console.log(obj)
     let sql='update cz set ? where cid=?'
    pool.query(sql,[obj,cid],(err,result)=>{
        if(err) {
            throw err
        }
        console.log(result)
		// 成功结果是对象，如果对象下的changedRows值为0说明修改失败，否则修改成功
		if(result.changedRows===0){
			res.send({code:400,msg:'修改失败'})
		}else{
			res.send({code:200,msg:'修改成功'})
		}
    }) 
})
//删除一条唇妆 http://localhost:3000/del/cz
app.post('/del/cz',(req,res)=>{
    let cid=req.query.cid
    console.log(cid)
    res.send('删除数据')
    let sql='delete from cz where cid=?'
    pool.query(sql,[cid],(err,result)=>{
        if(err) {
            throw err
        }
		// 删除结果是对象，判断对象下affectedRows值
		// 判断是否删除成功
		if(result.affectedRows===0) {
			res.send({code:400,msg:'删除失败'})
		}else{
            console.log(result)
            res.send({code:200,msg:'删除成功'})
        }
    })
})

//查询所有护肤 http://localhost:3000/search/hf
app.get('/search/hf',(req,res)=>{
    let sql='select * from hf'
    pool.query(sql,(err, result)=>{
        if(err){
            throw err
        }
        res.send({code:200,msg:'查询成功',data: result})
    })
})
//查询护肤按分类 http://localhost:3000/search/hf
app.get('/search/hf/:lx',(req,res)=>{
    let obj = req.params
    let sql='select * from hf where lx=?'
    pool.query(sql,[obj.lx],(err, result)=>{
        if(err){
            throw err
        }
        res.send({code:200,msg:'查询成功',data: result})
    })
})
//查询护肤名字详情 http://localhost:3000/detail/hf
app.get('/detail/hf/:name',(req,res)=>{
    let obj = req.params
    let sql='select * from hf where name=?'
    pool.query(sql,[obj.name],(err, result)=>{
        if(err){
            throw err
        }
        res.send({code:200,msg:'查询成功',data: result})
    })
})
//增加一条护肤 http://localhost:3000/add/hf
app.post('/add/hf',(req,res)=>{
    let obj = req.body
    let sql='insert into hf set ?'
    pool.query(sql,[obj],(err,result)=>{
        if(err) {
            throw err
        }
        res.send({code:200,msg:'添加成功',data:result})
    })
})


//修改一条护肤 http://localhost:3000/upd/hf
app.post('/upd/hf',(req,res)=>{
    // res.send('修改数据')
    // let obj=req.body
    let obj = Object.assign({},req.body)
    let hid=req.query.hid
    console.log(obj)
     let sql='update hf set ? where hid=?'
    pool.query(sql,[obj,hid],(err,result)=>{
        if(err) {
            throw err
        }
        console.log(result)
		// 成功结果是对象，如果对象下的changedRows值为0说明修改失败，否则修改成功
		if(result.changedRows===0){
			res.send({code:400,msg:'修改失败'})
		}else{
			res.send({code:200,msg:'修改成功'})
		}
    }) 
})
//删除一条护肤 http://localhost:3000/del/hf
app.post('/del/hf',(req,res)=>{
    let hid=req.query.hid
    console.log(hid)
    res.send('删除数据')
    let sql='delete from hf where hid=?'
    pool.query(sql,[hid],(err,result)=>{
        if(err) {
            throw err
        }
		// 删除结果是对象，判断对象下affectedRows值
		// 判断是否删除成功
		if(result.affectedRows===0) {
			res.send({code:400,msg:'删除失败'})
		}else{
            console.log(result)
            res.send({code:200,msg:'删除成功'})
        }
    })
})




//查询所有刷具 http://localhost:3000/search/sj
app.get('/search/sj',(req,res)=>{
    let sql='select * from sj'
    pool.query(sql,(err, result)=>{
        if(err){
            throw err
        }
        res.send({code:200,msg:'查询成功',data: result})
    })
})
//查询刷具名字详情 http://localhost:3000/detail/sj
app.get('/detail/sj/:name',(req,res)=>{
    let obj = req.params
    let sql='select * from sj where name=?'
    pool.query(sql,[obj.name],(err, result)=>{
        if(err){
            throw err
        }
        res.send({code:200,msg:'查询成功',data: result})
    })
})
//查询刷具按分类 http://localhost:3000/seaech/sj
app.get('/search/sj/:lx',(req,res)=>{
    let obj = req.params
    let sql='select * from sj where lx=?'
    pool.query(sql,[obj.lx],(err, result)=>{
        if(err){
            throw err
        }
        res.send({code:200,msg:'查询成功',data: result})
    })
})
//增加一条刷具 http://localhost:3000/add/sj
app.post('/add/sj',(req,res)=>{
    let obj = req.body
    let sql='insert into sj set ?'
    pool.query(sql,[obj],(err,result)=>{
        if(err) {
            throw err
        }
        res.send({code:200,msg:'添加成功',data:result})
    })
})


//修改一条刷具 http://localhost:3000/upd/sj
app.post('/upd/sj',(req,res)=>{
    // res.send('修改数据')
    // let obj=req.body
    let obj = Object.assign({},req.body)
    let sid=req.query.sid
    console.log(obj)
     let sql='update sj set ? where sid=?'
    pool.query(sql,[obj,sid],(err,result)=>{
        if(err) {
            throw err
        }
        console.log(result)
		// 成功结果是对象，如果对象下的changedRows值为0说明修改失败，否则修改成功
		if(result.changedRows===0){
			res.send({code:400,msg:'修改失败'})
		}else{
			res.send({code:200,msg:'修改成功'})
		}
    }) 
})
//删除一条刷具 http://localhost:3000/del/sj
app.post('/del/sj',(req,res)=>{
    let sid=req.query.sid
    console.log(sid)
    res.send('删除数据')
    let sql='delete from sj where sid=?'
    pool.query(sql,[sid],(err,result)=>{
        if(err) {
            throw err
        }
		// 删除结果是对象，判断对象下affectedRows值
		// 判断是否删除成功
		if(result.affectedRows===0) {
			res.send({code:400,msg:'删除失败'})
		}else{
            console.log(result)
            res.send({code:200,msg:'删除成功'})
        }
    })
})

/**
 * 眼妆接口
 */

//查询所有眼妆 http://localhost:3000/yz/search
app.get('/yz/search', (req, res, next) => {
    let sql = 'select * from yz';
    pool.query(sql, (err, result) => {
      if (err) {
        return next(err);
      }
      res.send({ code: 200, msg: '查询成功', data: result });
    });
});
  
 
  // 按类型查询眼妆   http://localhost:3000/yz/type
app.get('/yz/type', (req, res) => {
    // 获取get传递的参数
    var obj = req.query;
    console.log(obj);
    // 执行SQL命令
    pool.query('select * from yz where lx=?', [obj.lx], (err, r) => {
      if (err) {
        throw err;
      }
      console.log(r);
      // 判断查询是否成功，如果结果是空数组说明该员工不存在，否则查询成功
      if (r.length === 0) {
        res.send({ code: 400, msg: '分类不存在' });
      } else {
        // 接口规范，返回的结果是对象，包含有状态码(人为规定)，消息说明;如果需要返回数据，添加查询的数据
        res.send({ code: 200, msg: '查询成功', data: r });
      }
    });
    // query属于异步操作，需要把响应的结果放入到回调函数的内部
});
  
  //查询眼妆名字
  // http://localhost:3000/detailyz/:name
app.get('/detailyz/:name',(req,res)=>{
    let obj = req.params
    console.log(obj)
    
    let sql='select * from yz where name=? '
    pool.query(sql,[obj.name],(err, result)=>{
        if(err){
            throw err
        }
        res.send({code:200,msg:'查询成功',data: result})
    })
})
  
  // 删除眼妆接口
  // 接口地址：http://localhost:3000/v1/yz/del/:yid
  // 请求方法：delete
app.delete('/yz/del/:yid', (req, res) => {
    // 获取params传递的参数
    var obj = req.params;
    console.log(obj);
    // 执行SQL命令，删除编号对应的员工
    pool.query('delete from yz where yid=?', [obj.yid], (err, r) => {
      if (err) {
        throw err;
      }
      console.log(r);
      // 删除结果是对象，判断对象下affectedRows值
      // 判断是否删除成功
      if (r.affectedRows === 0) {
        res.send({ code: 400, msg: '删除失败' });
      } else {
        res.send({ code: 200, msg: '删除成功' });
      }
    });
});
  
  // 添加眼妆
  // 接口地址：http://localhost:3000/yz/emps
  // 请求方法：post
app.post('/yz/adds', (req, res) => {
    // 获取post传递的参数
    var obj = req.body;
    console.log(obj);
    // 执行SQL命令，将数据插入到数据库
    pool.query('insert into yz set ?', [obj], (err, results) => {
      if (err) {
        throw err;
      }
      console.log(results);
      // 把成功的结果响应
      if (results.length == 0) {
        //登录失败
        res.send({ message: 'login failed 添加失败', code: 201 });
      } else {
        //登录成功
        res.send({ message: 'ok 添加成功', code: 200, result: results[0] });
      }
    });
});
  
  //修改一条眼妆
  //http://localhost:3000/yz/upd
app.post('/yz/upd', (req, res) => {
    // res.send('修改数据')
    let obj = req.body;
    let yid = obj.yid;
    console.log(obj);
    console.log(yid);
    let sql = 'update yz set ? where yid=?';
    pool.query(sql, [obj, yid], (err, result) => {
      if (err) {
        throw err;
      }
      console.log(result);
      // 成功结果是对象，如果对象下的changedRows值为0说明修改失败，否则修改成功
      if (result.changedRows === 0) {
        res.send({ code: 400, msg: '修改失败' });
      } else {
        res.send({ code: 200, msg: '修改成功' });
      }
    });
});
  

/**
 * pro
 */

// 查询所有PRO
// http://localhost:3000/pro/search

app.get('/pro/search', (req, res, next) => {
    let sql = 'select * from pro';
    pool.query(sql, (err, result) => {
      if (err) {
        return next(err);
      }
      res.send({ code: 200, msg: '查询成功', data: result });
    });
  });
  
  //增加一条pro
  http://localhost:3000/pro/adds
  app.post('/pro/adds',(req,res)=>{
    let obj = req.body
    let sql='insert into pro set ?'
    console.log(obj)
    pool.query(sql,[obj],(err,result)=>{
        if(err) {
            throw err
        }
        res.send({code:200,msg:'添加成功',data:result})
    })
  })
  
  //修改一条pro列表
  //http://localhost:3000/pro/upd
  app.post('/pro/upd', (req, res) => {
    // res.send('修改数据')
    let obj = req.body;
    let pid = obj.pid;
    console.log(obj);
    console.log(pid);
    let sql = 'update pro set ? where pid=?';
    pool.query(sql, [obj, pid], (err, result) => {
      if (err) {
        throw err;
      }
      console.log(result);
      // 成功结果是对象，如果对象下的changedRows值为0说明修改失败，否则修改成功
      if (result.changedRows === 0) {
        res.send({ code: 400, msg: '修改失败' });
      } else {
        res.send({ code: 200, msg: '修改成功' });
      }
    });
  });
  
  // 按id删除pro
  // 接口地址：http://localhost:3000/peo/del/:pid
  // 请求方法：delete
  app.delete('/pro/del/:pid', (req, res) => {
    // 获取params传递的参数
    var obj = req.params;
    console.log(obj);
    // 执行SQL命令，删除编号对应的员工
    pool.query('delete from pro where pid=?', [obj.pid], (err, r) => {
      if (err) {
        throw err;
      }
      console.log(r);
      // 删除结果是对象，判断对象下affectedRows值
      // 判断是否删除成功
      if (r.affectedRows === 0) {
        res.send({ code: 400, msg: '删除失败' });
      } else {
        res.send({ code: 200, msg: '删除成功' });
      }
    });
  });

/* app.post('/login',(req,res)=>{
    let {phone,pwd}=req.query
    // let obj = Object.assign({},req.body)

    let sql="select * from user where phone=? and pwd=?"
    pool.query(sql,{phone,pwd},(err,result)=>{
        if(err){
            throw err
        }if(result.length==0){
            res.send({code:1001, msg:'账号密码输入错误'})
        }else{
            // 获取登录用户对象
            let user = result[0]
            // 为该用户颁发一个token字符串，未来该客户端若做发送其他请求，则需要在请求Header中携带token，完成状态管理。
            let payload = {id: user.id, nickname: user.nickname}
            let token = jwt.sign(payload, SECRET_KEY, {expiresIn: '1d'})
            // 返回user对象与token字符串
            user.pwd = undefined
            res.send({code:200,msg:'登录成功'})
        }
    })  
}) */
//登录 http://localhost:3000/userlogin
app.post('/userlogin',(req,res)=>{
    let obj = Object.assign({},req.body)

    let sql=`select * from user where phone=${obj.phone} and pwd=${obj.pwd}`
    pool.query(sql,[obj],(err,result)=>{
        if(err){
            throw err
        }else{
            res.send({code:200,msg:'登录成功',data:result})
        }
    })  
}) 
//注册 http://localhost:3000/register
app.post('/register',(req,res)=>{
    let obj = req.body
    let sql='insert into user set ?'
    pool.query(sql,[obj],(err,result)=>{
        if(err) {
            throw err
        }
        res.send({code:200,msg:'注册成功',data:result})
    })
})
//修改 http://localhost:3000/update
app.post('/update',(req,res)=>{
    // res.send('修改数据')
    // let obj=req.body
    let obj = Object.assign({},req.body)
    let id=req.query.id
    console.log(obj)
     let sql='update user set ? where id=?'
    pool.query(sql,[obj,id],(err,result)=>{
        if(err) {
            throw err
        }
        console.log(result)
		// 成功结果是对象，如果对象下的changedRows值为0说明修改失败，否则修改成功
		if(result.changedRows===0){
			res.send({code:400,msg:'修改失败'})
		}else{
			res.send({code:200,msg:'修改成功'})
		}
    }) 
})
//登录 http://localhost:3000/adminlogin
app.post('/adminlogin',(req,res)=>{
    let obj = Object.assign({},req.body)

    let sql=`select * from user where phone=${obj.phone} and pwd=${obj.pwd}`
    pool.query(sql,[obj],(err,result)=>{
        if(err){
            throw err
        }else{
            res.send({code:200,msg:'登录成功',data:result})
        }
    })  
})